/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-27     86188       the first version
 */
#ifndef APPLICATIONS_HEADER_FILE_IMU_H_
#define APPLICATIONS_HEADER_FILE_IMU_H_
#include <stdint.h>

typedef union
{
    struct
    {
        int16_t x;
        int16_t y;
        int16_t z;
    };
    int16_t axis[3];
} Axis3i16;

typedef union
{
    struct
    {
        int32_t x;
        int32_t y;
        int32_t z;
    };
    int32_t axis[3];
} Axis3i32;

typedef union
{
    struct
    {
        float x;
        float y;
        float z;
    };
    float axis[3];
} Axis3f;

typedef struct
{
  int32_t x;
  int32_t y;
}Axis2i32;

typedef struct
{
  float x;
  float y;
}Axis2f;

/* imu数据集合 */
typedef struct
{
    Axis3f acce;    //加速度（G）
    Axis3f gyro;    //陀螺仪（deg/s）
    float  tem;
} imuData_t;

typedef struct
{
    Axis3f angle;   //期望角度（度）
    Axis3f speed;   //期望速度（cm/s）
    Axis3f accel;   //期望加速度（cm/s^2）
} Expect_Data_t;
typedef struct
{
    double E_n, E_n_1, D_n, D_n_1;
    uint16_t n;
}variance_t;
typedef enum
{
    Calibration_none    = 0,
    Calibration_failed  = 0x0F,
    Calibration_ing     = 0x01,
    Calibration_OK      = 0xFF
}Cali_State_t;
typedef struct
{
    Axis3i32 sum;
    Axis3f   zero;
    variance_t  variance[3];
    uint16_t cycleCount;
    Cali_State_t calibration_State;
}Calibration_t;

typedef struct
{
    Axis3i16*    rawAcce_adc;
    Axis3i16*    rawGyro_adc;
    Axis3i16*    ripeAcce_adc;
    Axis3i16*    ripeGyro_adc;
    imuData_t*   imu_data;

    Calibration_t* CaliAcce;
    Calibration_t* CaliGyro;

    struct rt_mailbox* mb;

    void (*getRawData)(void);
    uint8_t (*acceCali)(Axis3i16 *acce_raw_adc);
    uint8_t (*gyroCali)(Axis3i16 *gyro_raw_adc);
    uint8_t (*acceUpdate)(Axis3f *acce);
    uint8_t (*gyroUpdate)(Axis3f *gyro);


}IMU_HandleTypedef;
extern IMU_HandleTypedef IMU_Handle;

void sensor_imuTask(void* parameter);

#endif /* APPLICATIONS_HEADER_FILE_IMU_H_ */
